package com.link.play.fit.ai.ui.adjust.adjustPage.fragment

import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModelProvider
import com.link.play.fit.ai.R
import com.link.play.fit.ai.base.BaseFragment
import com.link.play.fit.ai.bean.UserGuide
import com.link.play.fit.ai.databinding.AdjustPageFragment1Binding
import com.link.play.fit.ai.ui.adjust.adjustPage.AdjustPageModel

class AdjustPageFragment1 :
    BaseFragment<AdjustPageFragment1Binding>(AdjustPageFragment1Binding::inflate) {

    private var mWeekHabit = -1
    private val viewModel: AdjustPageModel by lazy {
        ViewModelProvider(requireActivity())[AdjustPageModel::class.java]
    }
//    private var recentlyGoalData: RecentlyGoalData? = null
    private var userGuide: UserGuide? = null

    // 创建视图组件集合
    private val contentLayouts by lazy { listOf(
        Triple(binding.contentLayout1, binding.choseNameText1, binding.choseDesText1),
        Triple(binding.contentLayout2, binding.choseNameText2, binding.choseDesText2),
        Triple(binding.contentLayout3, binding.choseNameText3, binding.choseDesText3)
    ) }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initData()
        initListener()
    }

    private fun initData() {
        userGuide = viewModel.getGoalData()
        userGuide?.let {
            mWeekHabit = it.workouts
            initBackGr(mWeekHabit)

        }
//        mWeekHabit = GlobalConfig.weekHabit
        initBackGr(mWeekHabit)
    }

    private fun initBackGr(position: Int) {
        // 统一重置所有视图状态
        contentLayouts.forEach { (layout, nameText, desText) ->
            layout.setBackgroundResource(R.drawable.bg_a_style_07)
            activity?.run {
                nameText.setTextColor(getColor(R.color.black_11))
                desText.setTextColor(getColor(R.color.black_11))
            }
        }

        // 处理选中状态
        contentLayouts.getOrNull(position)?.let { (layout, nameText, desText) ->
            layout.setBackgroundResource(R.drawable.bg_a_style_07_green_r10)
            activity?.run {
                nameText.setTextColor(getColor(R.color.white))
                desText.setTextColor(getColor(R.color.white))
            }
        }

        // 更新 ViewModel
        viewModel.run {
            setNextPage(position != -1)
            setGoalsValue(weekHabit = position)
        }
    }

    private fun initListener() {
        // 使用位置索引简化点击监听
        contentLayouts.forEachIndexed { index, (layout, _, _) ->
            layout.setOnClickListener { initBackGr(index ) }
        }
    }

}